import pickle


def serialize(obj):
    return pickle.dumps(obj)


def deserialize(data):
    try:
        return pickle.loads(data)
    except pickle.UnpicklingError:
        pass


class DownloadRequest:
    def __init__(self, begpos, endpos, filepath):
        self.begpos = begpos
        self.endpos = endpos
        self.filepath = filepath

    def __str__(self):
        return str.format("begpos:{},endpos:{},filepath:{}", self.begpos, self.endpos, self.filepath)

class GetWorkerInfoRequest:
    def __init__(self, filepath):
        self.filepath = filepath

    def __str__(self):
        return str.format("filepath:{}", self.filepath)

class WorkerInfoRespond:
    def __init__(self, port, filesize, errormsg):
        self.port = port
        self.filesize = filesize
        self.errormsg = errormsg

    def __str__(self):
        return str.format("port:{},filesize:{},errormsg:{}", self.port, self.filesize, self.errormsg)
